package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.JList;
import java.awt.Color;
import javax.swing.JEditorPane;
import javax.swing.ListSelectionModel;
import java.awt.TextArea;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import ocsf.server.ConnectionToClient;
import messages.LoginMessage;
import controllers.ClientController;
import controllers.DBController;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;

import entity.UserLoginInfo;

public class CustomerComplaint extends JFrame {
	
	ClientController clientCon;
	private JPanel contentPane;
	private JTextPane textFieldComp;
	static String ID;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CustomerComplaint frame = new CustomerComplaint(ID);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Customer Complaint frame.
	 */
	public CustomerComplaint(String id) {
		this.ID=id;
		System.out.println("CustomerComplaint:  "+this.ID);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 629, 365);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnConf = new JButton("Confirm");
		btnConf.setBounds(129, 239, 100, 23);
		btnConf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {            /*confirm button action */
				try{
					btnConfActionPerformed(e);
					dispose();
				}catch(IOException | SQLException e1) {
					e1.printStackTrace();
				}
			}			
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(349, 239, 84, 23);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CustomerComplaint.this.setVisible(false);

			}
		});
		
		JLabel lblComplaint = new JLabel("Complaint");
		lblComplaint.setBounds(250, 16, 100, 22);
		lblComplaint.setFont(new Font("Tahoma", Font.ITALIC, 18));
		
		textFieldComp = new JTextPane();
		textFieldComp.setText(" ");
		textFieldComp.setBounds(127, 49, 436, 158);
		textFieldComp.setToolTipText("");
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(239, 239, 100, 23);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CustomerGui frame = new CustomerGui(ID);
				frame.setVisible(true);
				dispose();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(btnCancel);
		contentPane.add(lblComplaint);
		contentPane.add(btnConf);
		contentPane.add(btnBack);
		contentPane.add(textFieldComp);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images\\tmuna4.jpg"));
		lblNewLabel.setBounds(0, 0, 613, 326);
		contentPane.add(lblNewLabel);
		contentPane.setLayout(null);
		
		JLabel lblInsertYourComplaint = new JLabel("Insert your complaint : ");
		lblInsertYourComplaint.setFont(new Font("Miriam", Font.PLAIN, 12));
		lblInsertYourComplaint.setBounds(10, 55, 131, 23);
		contentPane.add(lblInsertYourComplaint);
	}
	/**
	 * transfer the complain to the server
	 * @param ev
	 * @throws IOException
	 * @throws SQLException
	 */
	
	private void btnConfActionPerformed(ActionEvent ev) throws IOException, SQLException {
		UserLoginInfo logi=new UserLoginInfo();
		String ip=logi.getIP();
		
		
		ArrayList<String> arr=new ArrayList<String>();
		
		int port=5555;
		String complaint=textFieldComp.getText();
		arr.add("Complaint");
		arr.add(this.ID);
		arr.add(complaint);
		this.clientCon=new ClientController(ip,port);		
		clientCon.handleMessageFromGui(arr);
		JOptionPane.showMessageDialog(null, "Thank you the Comlaint sent successfuly ");
		CustomerGui frame = new CustomerGui(ID);
		frame.setVisible(true);
	}
}
